package club.emergency.wechat.staff.search.consumer.controller;

import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.staff.search.consumer.service.StaffSearchConsumer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.staff.search.consumer.controller
 * Date: 2018/11/16
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@Api("用户搜索模块，包括登录等")
@RestController
@RequestMapping("/staff-search")
public class StaffSearchController {

    private StaffSearchConsumer staffSearchConsumer;

    public StaffSearchController(StaffSearchConsumer staffSearchConsumer) {
        this.staffSearchConsumer = staffSearchConsumer;
    }

    @ApiOperation("按条件查询用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(value = "每页显示最大数据量", name = "pageSize", dataType = "integer", paramType = "query")
    })
    @PostMapping("/flipList")
    public ResponseEntity flipList(
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(staffSearchConsumer.flipList(pageNo, pageSize), ResponseEntity.class);
    }

    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "微信openId", name = "openId", dataType = "string", paramType = "query"),
    })
    @PostMapping("/signature")
    public ResponseEntity signature(
            HttpServletResponse httpServletResponse,
            @RequestParam("openId") String openId
    ) {
        String signature = staffSearchConsumer.signature(openId);
        ResponseEntity responseEntity = new JsonConvert<ResponseEntity>().jsonDecode(signature, ResponseEntity.class);
        if (responseEntity.getStatusCode() != StatusCode.LOGIN_OK) {
            return responseEntity;
        }
        //把数据放在authorization中
        httpServletResponse.setHeader("Authorization", new JsonConvert<>().jsonEncode(responseEntity.getData()));
        return responseEntity;
    }

    @ApiOperation("更新Token")
    @PostMapping("/updateToken")
    public ResponseEntity updateToken(HttpServletResponse httpServletResponse) {
        String updateToken = staffSearchConsumer.updateToken();
        ResponseEntity responseEntity = new JsonConvert<ResponseEntity>().jsonDecode(updateToken, ResponseEntity.class);
        if (responseEntity.getStatusCode() != StatusCode.LOGIN_OK) {
            return responseEntity;
        }
        //把数据放在authorization中
        httpServletResponse.setHeader("Authorization", new JsonConvert<>().jsonEncode(responseEntity.getData()));
        return responseEntity;
    }

    @ApiOperation("检查每日答题是否完成")
    @ApiImplicitParam(value = "id", name = "id", dataType = "long", paramType = "query")
    @PostMapping("/checkEachTest")
    public ResponseEntity checkEachTest(@RequestParam("id") Long id) {
        return new JsonConvert<ResponseEntity>().jsonDecode(staffSearchConsumer.checkEachTest(id), ResponseEntity.class);
    }
}
